//
//  ModifyPasswordsViewController.m
//  lhpc_manager
//
//  Created by 李英杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "ModifyPasswordsViewController.h"
#import "InputPassWordView.h"
#import "LHNetworking.h"

@interface ModifyPasswordsViewController ()

@property (nonatomic ,strong) InputPassWordView * oldPassWordView;

@property (nonatomic ,strong) InputPassWordView * setNewPassWordView;

@property (nonatomic ,strong) InputPassWordView * confirmNewPassWordView;

@property (nonatomic, strong) UIButton * confirmButton;

@end

@implementation ModifyPasswordsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.navigationItem.title = @"修改密码";
    
    [self setMainView];
    
}


-(void)comfirmButtonAction{
    
    if (self.oldPassWordView.passwordTextField.text.length == 0) {
        [HLProgressHUD showHudTipStr:@"请输入旧密码"];
        return;
    }
    
    if (self.oldPassWordView.passwordTextField.text.length != 6) {
        [HLProgressHUD showHudTipStr:@"请确认旧密码位数"];
        return;
    }
    
    if (self.setNewPassWordView.passwordTextField.text.length == 0 || self.confirmNewPassWordView.passwordTextField.text.length == 0) {
        [HLProgressHUD showHudTipStr:@"新密码不能为空"];
        return;
    }
    
    if (self.setNewPassWordView.passwordTextField.text.length != 6 || self.confirmNewPassWordView.passwordTextField.text.length != 6) {
        [HLProgressHUD showHudTipStr:@"请确认新密码为6位"];
        return;
    }
    
    if (![self.setNewPassWordView.passwordTextField.text isEqualToString:self.confirmNewPassWordView.passwordTextField.text]) {
        [HLProgressHUD showHudTipStr:@"两次输入密码不一致"];
        return;
    }
    
    NSDictionary * parameters = @{@"oldPassword":[self.oldPassWordView.passwordTextField.text hl_md5],
                                  @"newPassword1":[self.setNewPassWordView.passwordTextField.text hl_md5],
                                  @"newPassword2":[self.confirmNewPassWordView.passwordTextField.text hl_md5]};
    
    LHPC_WEAK_SELF;
    [[LHNetworking shared] POST:LHSetRequestAPI(LHPasswordUpdate) parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        LHPC_STRONG_SELF;
        
        if (responseObject) {
            
            [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleSuccess status:@"提现密码重置成功!"];
            
            [strongSelf.navigationController popToRootViewControllerAnimated:YES];
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [HLProgressHUD showHudTipStr:LHNetWorkErrorMessage];
    }];
}

-(void)setMainView{
    self.view.backgroundColor = BACKCOLOR;
    
    [self.view addSubview:self.oldPassWordView];
    [self.view addSubview:self.setNewPassWordView];
    [self.view addSubview:self.confirmNewPassWordView];
    [self.view addSubview:self.confirmButton];
    
    [self updateViewConstraints];
}



-(void)updateViewConstraints{
    [super updateViewConstraints];
    
    [self.oldPassWordView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view).offset(10);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@55);
        
    }];
    
    
    [self.setNewPassWordView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.oldPassWordView.mas_bottom).offset(10);
        make.left.right.equalTo(self.view);
        make.height.equalTo(self.oldPassWordView);
    }];
    
    [self.confirmNewPassWordView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.setNewPassWordView.mas_bottom).offset(1);
        make.left.right.equalTo(self.view);
        make.height.equalTo(self.oldPassWordView);
    }];
    
    [self.confirmButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.confirmNewPassWordView.mas_bottom).offset(50);
        make.left.equalTo(self.view).offset(30);
        make.right.equalTo(self.view).offset(-30);
        make.height.equalTo(@40);
        
        
    }];
    
    
}

-(InputPassWordView *)oldPassWordView{
    if (!_oldPassWordView) {
        _oldPassWordView = [[InputPassWordView alloc]init];
        _oldPassWordView.titleStr = @"旧密码";
        _oldPassWordView.placeholderStr = @"请输入旧密码";
    }
    return _oldPassWordView;
}

-(InputPassWordView *)setNewPassWordView{
    if (!_setNewPassWordView) {
        _setNewPassWordView = [[InputPassWordView alloc]init];
        _setNewPassWordView.titleStr = @"新密码";
        _setNewPassWordView.placeholderStr = @"请输入新的6位数字密码";
    }
    return _setNewPassWordView;
}

-(InputPassWordView *)confirmNewPassWordView{
    if (!_confirmNewPassWordView) {
        _confirmNewPassWordView = [[InputPassWordView alloc]init];
        _confirmNewPassWordView.titleStr = @"确认新密码";
        _confirmNewPassWordView.placeholderStr = @"请确认新的6位数字密码";
    }
    return _confirmNewPassWordView;
}

-(UIButton *)confirmButton{
    if (!_confirmButton) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmButton.layer.cornerRadius = 3;
        [_confirmButton setBackgroundColor:ORANGCOLOR];
        [_confirmButton setTitle:@"确认修改" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_confirmButton addTarget:self action:@selector(comfirmButtonAction) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.userInteractionEnabled = YES;
        
    }
    return _confirmButton;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.oldPassWordView.passwordTextField becomeFirstResponder];
}

@end
